package com.barneyx.mshinfoplatform.common;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.barneyx.mshinfoplatform.entity.admin.AdminUserEntity;
import com.barneyx.mshinfoplatform.entity.enums.EUserState;
import com.barneyx.mshinfoplatform.exception.AdminUserException;
import com.barneyx.mshinfoplatform.service.admin.IAdminUserEntityService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class AdminServiceUtil {
    @Resource
    private IAdminUserEntityService adminEntityService;

    public AdminUserEntity login(String username, String userpwd) throws AdminUserException {
        AdminUserEntity admin_user = adminEntityService.getOne(new LambdaQueryWrapper<AdminUserEntity>()
                .eq(AdminUserEntity::getUserName, username)
                .eq(AdminUserEntity::getUserPwd, userpwd));
        if (admin_user == null) throw new AdminUserException(ExResult.ResponseCode.USERNAME_OR_PASSWORD_ERROR);
        if (admin_user.getUserState() == EUserState.LOCK)
            throw new AdminUserException(ExResult.ResponseCode.ACCESS_USER_LOCK);
        StpUtil.login(admin_user.getAdminId());
        return admin_user;
    }
}
